package cn.taoshop.user.service;


import java.util.Date;
import java.util.List;



import org.apache.struts2.ServletActionContext;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;



import cn.taoshop.order.vo.Order;
import cn.taoshop.user.dao.UserDao;
import cn.taoshop.user.vo.User;
import cn.taoshop.utils.MailUtil;
import cn.taoshop.utils.PageBean;
import cn.taoshop.utils.UUIDUtils;

// 用户名模块业务层代码

@Transactional
public class UserService {
	// 注入UserDao
	private UserDao userDao;

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	
	
	// 按用户名查询用户的方法:
	public User findByUsername(String username){
		return userDao.findByUsername(username);
	}

	// 业务层完成用户注册代码:

	public void save(User user) {
		// 将数据存入到数据库
		user.setUsername(user.getUsername());
		user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
		user.setState(0); // 0:代表用户未激活.  1:代表用户已经激活.
		String code = UUIDUtils.getUUID()+UUIDUtils.getUUID();
		user.setCode(code);
		long outDate = (System.currentTimeMillis() + 30 * 60 * 1000)/1000 * 1000;// 30分钟后过期
        
        Date date = new Date(outDate);
		user.setOutdate(date);
		userDao.save(user);
		
		
		
		
		String key = user.getUsername()+"$"+user.getCode();
		String digitalSignature = DigestUtils.md5DigestAsHex(key.getBytes());// 数字签名
		String path = ServletActionContext.getRequest().getContextPath();
		String basePath =ServletActionContext.getRequest().getScheme()+"://"
				+ServletActionContext.getRequest().getServerName()+":"
				+ServletActionContext.getRequest().getServerPort()+path+"/";
		String resethref = basePath+"user_active.action?sid="+
				digitalSignature+"&userName="+user.getUsername();
		
		
		String subject="注册账号激活";
		String emailContent="请勿回复本邮件，点击下面链接，激活账号<br/><a href="
				+resethref+" target='_BLANK'>点击我激活</a>"+resethref+"</a>"+"<br/>tips:本邮件超过30分钟，链接将会失效！";
		// 发送激活邮件;
		MailUtil.sendMail(user.getEmail(),subject,emailContent);
	}

	// 业务层根据激活码查询用户
	public User findByCode(String code) {
		return userDao.findByCode(code);
	}

	// 修改用户的状态的方法
	public void update(User existUser) {
		userDao.update(existUser);
	}
	//修改密码
	public void updatePwd(User user){
		
		
		User temp = findByUsername(user.getUsername());
		temp.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
		userDao.updatePwd(temp);
	}

	// 用户登录的方法
	public User login(User user) {
		user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
		return userDao.login(user);
	}
	
	//查看用户状态
	public int checkuserstate(User user){
		int state=0;
		state =userDao.checkuserstate(user);
		return state;
	}
	
	//校验邮箱是否已注册
	public int checkEmail(String email,int state){
		int count = 0;
		count = userDao.checkEmail(email,state);  //state 1是已激活， 0未激活
		return count;
	}

	// 业务层用户查询所有
	public PageBean<User> findByPage(Integer page) {
		PageBean<User> pageBean = new PageBean<User>();
		// 设置当前页数:
		pageBean.setPage(page);
		// 设置每页显示记录数:
		// 显示5个
		int limit = 5;
		pageBean.setLimit(limit);
		// 设置总记录数:
		int totalCount = 0;
		totalCount = userDao.findCount();
		pageBean.setTotalCount(totalCount);
		// 设置总页数
		int totalPage = 0;
		if(totalCount % limit == 0){
			totalPage = totalCount / limit;
		}else{
			totalPage = totalCount / limit + 1;
		}
		pageBean.setTotalPage(totalPage);
		// 设置每页显示数据集合:
		int begin = (page - 1)*limit;
		List<User> list = userDao.findByPage(begin,limit);
		pageBean.setList(list);
		return pageBean;
	}


	public User findByUid(Integer uid) {
		return userDao.findByUid(uid);
	}
	
	public User findByUser(User user){
		return userDao.findByUser(user);
	}
	public int checkEmailByUser(User user,int state){
		int count = userDao.checkEmailByUser(user, state);
		return count;
	}

	public void delete(User existUser) {
		userDao.delete(existUser);
	}
}
